#include "daoword.h"

#include <QtSql>

#include "sql.h"
#include "utildb.h"

void WordDao::insert(const QList<Word>& words)
{
    DbUtil& dbUtil = DbUtil::getInstance();
    QSqlQuery query(dbUtil.getDatabase());

    //插入单词数据
    query.prepare(SQL_INSERT_WROD);
    for(auto& word : words)
    {
        query.addBindValue(QString::fromStdString(word.original()));
        query.addBindValue(QString::fromStdString(word.translation()));
        query.addBindValue(word.vocabularyId());
        query.exec();
    }
    dbUtil.close();
}

QList<Word> WordDao::queryAll()
{
    QList<Word> words;

    DbUtil& dbUtil = DbUtil::getInstance();
    QSqlQuery query(dbUtil.getDatabase());

    query.exec(SQL_SELECT_WORD_ALL);

    Word *word;
    while (query.next()) {
        word = new Word();
        word->parseRecord(query.record());
        words.append(*word);
    }

    dbUtil.close();
    return words;
}

WordDao::WordDao()
{

}
